<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Project extends CI_Controller {

        public function __construct()
        {
            parent::__construct();
            $this->load->model('project_model');
            $this->load->helper('form');
            $this->load->library('form_validation');
        }

        // 项目列表
        public function prolist()
        {
            // 检查是否登录
            if($this->session->userdata('logged_in') !== TRUE)
            {
                redirect('user/login');
            }

            // 如为搜索 设置关键词参数 否则关键词为空
            if ($this->input->post('keyword') != '')
            {
                $keyword = $this->input->post('keyword');
            }
            else if ($this->input->get('keyword') != '')
            {
                $keyword = $this->input->get('keyword');
            }
            else
            {
                $keyword = '';
            }

            // 加载分页类
            $this->load->library('pagination');

            // 指定分页参数
            if ($keyword != '')
            {
                $config['page_query_string'] = TRUE;
                $config['query_string_segment'] = 'offset';
                $config['base_url'] = site_url('project/prolist?keyword='.$keyword);
                $config['first_url'] = site_url('project/prolist?keyword='.$keyword);
            }
            else
            {
                $config['base_url'] = site_url('project/prolist');
                $config['first_url'] = site_url('project/prolist');
            }
            
            $config['total_rows'] = $this->project_model->record_count($keyword); // 记录总数
            $config['per_page'] = 1; // 每页记录数
            $config['num_links'] = 2; //当前页左右显示的数字链接数
           
            // 初始化分页类
            $this->pagination->initialize($config);
     
            // 创建分页链接
            $data['page_links'] = $this->pagination->create_links();

            // 接收页码参数
            if ($keyword != '')
            {
                $page = ($this->input->get('offset') != '') ? $this->input->get('offset') : 1;
            }
            else
            {
                $page = ($this->uri->segment(3) != '') ? $this->uri->segment(3) : 1;
            }

            // 检索数据
            $data['projects'] = $this->project_model->get_all($config['per_page'], $page, $keyword);

            $this->load->view('templates/header');
            $this->load->view('project/list', $data);
            $this->load->view('templates/footer');
        }

        // 创建项目
        public function create()
        {
            if($this->session->userdata('logged_in') !== TRUE)
            {
                redirect('user/login');
            }
            
            if($this->form_validation->run() === FALSE)
            {
                $this->load->view('templates/header');
                $this->load->view('project/create');
                $this->load->view('templates/footer');
            }
            else
            {
                $proname = $this->input->post('proname');
                $starttime = $this->input->post('starttime');

                if($this->project_model->create($proname, $starttime))
                {
                    redirect('project/prolist');
                }
            }
        }

        // 查看项目详情(项目基本信息及进度)
        public function view()
        {
            // 项目ID
            $pid = $this->uri->segment(3);

            // 获取项目信息
            $data['project_info'] = $this->project_model->get_one($pid);

             // 分页显示项目进度
            $this->load->library('pagination');
            $config['page_query_string'] = TRUE;
            $config['query_string_segment'] = 'offset';
            $config['base_url'] = base_url() . 'project/view/'.$pid;
            $config['total_rows'] = $this->project_model->progress_count($pid);
            $config['per_page'] = 10;
            $config['num_links'] = 2;
            $this->pagination->initialize($config);

            $offset = $this->input->get('offset');
            $data['page_links'] = $this->pagination->create_links();

            // 定义页码
            if(isset($offset))
            {
                $page = $offset;
            }
            else
            {
                $page = 1;
            }

            // 获取项目进度
            $data['project_progress'] = $this->project_model->get_all_progress($config['per_page'], $page, $pid);

            // 向管理员和客户显示不同信息
            if($this->session->userdata('logged_in') == TRUE)
            {
                $this->load->view('templates/header');
                $this->load->view('project/view', $data);
                $this->load->view('templates/footer');
            }
            else
            {
                $this->load->view('templates/header_customer');
                $this->load->view('project/view', $data);
                $this->load->view('templates/footer_customer');
            }

            
        }

        // 编辑项目
        public function edit()
        {
            if($this->session->userdata('logged_in') !== TRUE)
            {
                redirect('user/login');
            }
            
            if($this->form_validation->run() === FALSE)
            {
                $data['project'] = $this->project_model->get_one($this->uri->segment(3));

                $this->load->view('templates/header');
                $this->load->view('project/edit', $data);
                $this->load->view('templates/footer');
            }
            else
            {
                $proname = $this->input->post('proname');
                $starttime = $this->input->post('starttime');
                $pid = $this->input->post('pid');

                if($this->project_model->edit($proname, $starttime, $pid))
                {
                    redirect('project/prolist');
                }
            }
        }

        // 删除项目
        public function delete($pid)
        {
            if($this->project_model->delete($pid))
            {
                redirect('project/prolist');
            }
        }

        // 添加进度
        public function addProgress()
        {
            if($this->session->userdata('logged_in') !== TRUE)
            {
                redirect('user/login');
            }
            
            if($this->form_validation->run() === FALSE)
            {
                $data['proid'] = $this->uri->segment(3);

                $this->load->view('templates/header');
                $this->load->view('project/addProgress', $data);
                $this->load->view('templates/footer');
            }
            else
            {
                $content = $this->input->post('content');
                $writetime = $this->input->post('writetime');
                $proid = $this->input->post('proid');

                if($this->project_model->add_progress($content, $writetime, $proid))
                {
                    redirect('project/prolist');
                }
            }

        }

        // 编辑进度
        public function editProgress()
        {
            if($this->session->userdata('logged_in') !== TRUE)
            {
                redirect('user/login');
            }
            
            if($this->form_validation->run() === FALSE)
            {
                $data['progress'] = $this->project_model->get_progress($this->uri->segment(3));

                $this->load->view('templates/header');
                $this->load->view('project/editProgress', $data);
                $this->load->view('templates/footer');
            }
            else
            {
                $content = $this->input->post('content');
                $writetime = $this->input->post('writetime');
                $progressid = $this->input->post('progressid');
                $pid = $this->input->post('pid');

                if($this->project_model->edit_progress($content, $writetime, $progressid))
                {
                    redirect('project/view/'.$pid);
                }
            }
        }

        // 删除进度
        public function delProgress($id)
        {
            $progress = $this->project_model->get_progress($id);
            $pid = $progress['pid'];

            if($this->project_model->del_progress($id))
            {
                redirect('project/view/'.$pid);
            }
        }

}
